#include <stdio.h>
#include <string.h>

void recycle(int** array,int n)
{
    for(int i=0;i<n;i++)
    {
        delete array[i];
    }
    delete array;
}

int match(int i,int** array,int* bj,int* vis,int n){

    for(int j=0;j<n;j++)
    {
        if(array[i][j]&&!vis[j]){ //这个点没有访问过
            vis[j]=1; //标记成已访问
            //如果暂时没有匹配或者能匹配到别的点
            if(bj[j]==-1||match(bj[j],array,bj,vis,n))
            {
                bj[j]=i;
                return 1;
            }
        }
    }
    return 0;
}

int pipei(int** array,int n)
{
    int i,j;
    int* bj=new int[n];
    int* use=new int[n];
    int size=sizeof(int)*n;
    memset(bj,-1,size);

    int result=0;

    for(i=0;i<n;i++)
    {
        memset(use,0,size);
        if(match(i,array,bj,use,n))
        {
            result++;
        }
    }

    delete bj;
    delete use;
    return result;
}



int main(){
    int t,n;
    scanf("%d",&t);
    int i,j,k;
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        int** array=new int*[n];
        for(j=0;j<n;j++)
        {
            array[j]=new int[n];
            for(k=0;k<n;k++)
            {
                scanf("%d",&array[j][k]);
            }
        }

        int result=pipei(array,n);
        // printf("%d\n",result);
        if(result==n)
        {
            printf("Yes\n");
        }
        else{
             printf("No\n");
        }
        

        recycle(array,n);
    }
    return 0;
}